﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HtmlAgilityPack;
using System.Text;
using YinYueTai.Model;
using HaoLab.WindowsPhone;

namespace YinYueTai.HtmlParser
{
    public class Video
    {
        private string page;

        private Action<MvDetail> act;

        public Video(string page, Action<MvDetail> act)
        {
            this.page = page;
            this.act =  act;
        }

        private MvDetail mv;

        public void GetInfo(MvDetail mv)
        {
            GlobalIndicator.Instance.Text = "正在加载...";
            GlobalIndicator.Instance.IsBusy = true;
            this.mv = mv;
            WebClient wc = new WebClient();
            wc.Headers["Accept"] = "text/html, application/xhtml+xml, */*";
            //wc.Headers["Accept-Encoding"] = "gzip, deflate";
            wc.Headers["User-Agent"] = "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; Microsoft; XDeviceEmulator)";
            wc.Headers["Accept-Language"] = "zh-cn,en-us;q=0.5";
            wc.Headers["UA-CPU"] = "x86";
            wc.Headers["Referer"] = "http://www.yinyuetai.com/";
            
            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
            wc.DownloadStringAsync(new Uri("http://www.yinyuetai.com" + page));
          
        }

        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                HtmlDocument html = new HtmlDocument();
                html.LoadHtml(e.Result);

                var node = html.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[8]/div[1]/video[1]");
                mv.VidoeSrc = node.Attributes["src"].Value;

                node = html.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[9]/div[1]/div[3]/div[1]/span[1]");
                mv.PlayTimes = node.InnerText.Trim();

                node = html.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[9]/div[1]/div[3]/div[1]/div[1]/span[1]");
                mv.UploadTime = node.InnerText.Trim();

                node = html.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[9]/div[1]/div[3]/p[1]");
                mv.Detail = node.InnerText.Trim();
                act.Invoke(mv);
                
                GlobalIndicator.Instance.IsBusy = false;
                GlobalIndicator.Instance.Text = String.Empty;
            }
            catch (Exception ex)
            {

                if (MessageBox.Show("出错了！请检查网络连接，是否查看错误信息？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    MessageBox.Show(ex.Message);
                return;
            }
           

        }

       
    }
}
